package com.beauty.dressing.logic.dao;

import com.framework.common.utils.IDateFormatUtil;
import com.beauty.dressing.common.bean.ShopCarBean;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.logic.db.DaoSession;
import com.beauty.dressing.logic.db.DbLoginUser;
import com.beauty.dressing.logic.db.DbShopCar;

import java.util.List;

import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.QueryBuilder;

/**
 * 登录用户表的数据库操作类
 *
 * @author xutingz
 * @company xiaolanba.com
 */
public class ShopCarDao extends DbShopCar {

    public ShopCarDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * 用户登录
     *
     * @param shopCar
     */
    public void insertShopCar(ShopCarBean shopCar) {
        try {
            if (shopCar != null) {
                shopCar.insertTime = IDateFormatUtil.getTimeMillis();
                insert(shopCar);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户登出
     */
    public void clear() {
        try {
            deleteAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询登录用户
     */
    public List<ShopCarBean> queryAll() {
        try {
            QueryBuilder<ShopCarBean> queryBuilder = queryBuilder();
            return queryBuilder.list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 更新用户信息,比如昵称、头像、手机号等更改了
     */
    public void updateShopCar(ShopCarBean loginUser) {
        try {
            if (loginUser != null) {
                ShopCarBean dbLoginUser = queryShopCarById(loginUser.id);
                if (dbLoginUser != null) {
                    loginUser._id = dbLoginUser._id;
                    loginUser.insertTime = IDateFormatUtil.getTimeMillis();
                    update(loginUser);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据用户id查询
     *
     * @return
     */
    public ShopCarBean queryShopCarById(long id) {

        try {
            QueryBuilder<ShopCarBean> queryBuilder = queryBuilder();
            queryBuilder.where(Properties.Id.eq(id));
            return queryBuilder.unique();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean deleteShopcar(long _id) {
        boolean b = false;
        try {
            QueryBuilder<ShopCarBean> queryBuilder = queryBuilder();
            queryBuilder.where(Properties._Id.eq(_id));
            queryBuilder.buildDelete().executeDeleteWithoutDetachingEntities();
            b = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return b;
    }
}
